package com.haltakov.letterjumble.ui;

import java.awt.FontMetrics;
import java.awt.Graphics;

public class GraphicHelpers {
	public static void drawStringCentered(Graphics g, FontMetrics metrics, String text, int x, int y, int width, int height) {
		int space = g.getFont().getSize()/3;
		
		String[] words = text.split("\n");
		
		int count = words.length;
		int sizeY = metrics.getHeight();
		int sizeX[] = new int[words.length];
		
		for (int i=0; i<count; ++i)
			sizeX[i] = metrics.stringWidth(words[i]);
		
		int cy = y + (height - (count * sizeY + (count-1)*space))/2 + sizeY - metrics.getDescent(); 
		
		for (int i=0; i<count; ++i) {
			int cx = x + (width-sizeX[i])/2;
			
			g.drawString(words[i], cx, cy);
			
			cy += sizeY + space;
		}
	}
}
